-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BlogDetailHeaderView.swift
388 lines (298 loc) · 13.8 KB
/
BlogDetailHeaderView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import Gridicons
import UIKit
@objc protocol BlogDetailHeaderViewDelegate {
func siteIconTapped()
func siteIconReceivedDroppedImage(_ image: UIImage?)
func siteIconShouldAllowDroppedImages() -> Bool
func siteTitleTapped()
func siteSwitcherTapped()
func visitSiteTapped()
}
class BlogDetailHeaderView: UIView {
// MARK: - Child Views
private let titleView: TitleView
// MARK: - Delegate
@objc weak var delegate: BlogDetailHeaderViewDelegate?
@objc var updatingIcon: Bool = false {
didSet {
if updatingIcon {
titleView.siteIconView.activityIndicator.startAnimating()
} else {
titleView.siteIconView.activityIndicator.stopAnimating()
}
}
}
@objc var blavatarImageView: UIImageView {
return titleView.siteIconView.imageView
}
@objc var blog: Blog? {
didSet {
refreshIconImage()
toggleSpotlightOnSiteTitle()
toggleSpotlightOnSiteUrl()
refreshSiteTitle()
if let displayURL = blog?.displayURL as String? {
titleView.set(url: displayURL)
}
titleView.siteIconView.allowsDropInteraction = delegate?.siteIconShouldAllowDroppedImages() == true
}
}
@objc func refreshIconImage() {
if let blog = blog,
blog.hasIcon == true {
titleView.siteIconView.imageView.downloadSiteIcon(for: blog)
} else if let blog = blog,
blog.isWPForTeams() {
titleView.siteIconView.imageView.tintColor = UIColor.listIcon
titleView.siteIconView.imageView.image = UIImage.gridicon(.p2)
} else {
titleView.siteIconView.imageView.image = UIImage.siteIconPlaceholder
}
toggleSpotlightOnSiteIcon()
}
func setTitleLoading(_ isLoading: Bool) {
isLoading ? titleView.titleButton.startLoading() : titleView.titleButton.stopLoading()
}
func refreshSiteTitle() {
let blogName = blog?.settings?.name
let title = blogName != nil && blogName?.isEmpty == false ? blogName : blog?.displayURL as String?
titleView.titleButton.setTitle(title, for: .normal)
}
func toggleSpotlightOnSiteTitle() {
titleView.titleButton.shouldShowSpotlight = QuickStartTourGuide.shared.isCurrentElement(.siteTitle)
}
func toggleSpotlightOnSiteUrl() {
titleView.subtitleButton.shouldShowSpotlight = QuickStartTourGuide.shared.isCurrentElement(.viewSite)
}
func toggleSpotlightOnSiteIcon() {
titleView.siteIconView.spotlightIsShown = QuickStartTourGuide.shared.isCurrentElement(.siteIcon)
}
private enum LayoutSpacing {
static let atSides: CGFloat = 20
static let top: CGFloat = 10
static let bottom: CGFloat = 16
static let belowActionRow: CGFloat = 24
static func betweenTitleViewAndActionRow(_ showsActionRow: Bool) -> CGFloat {
return showsActionRow ? 32 : 0
}
static let spacingBelowIcon: CGFloat = 16
static let spacingBelowTitle: CGFloat = 8
static let minimumSideSpacing: CGFloat = 8
static let interSectionSpacing: CGFloat = 32
static let buttonsBottomPadding: CGFloat = 40
static let buttonsSidePadding: CGFloat = 40
static let maxButtonWidth: CGFloat = 390
static let siteIconSize = CGSize(width: 48, height: 48)
}
// MARK: - Initializers
required init(items: [ActionRow.Item]) {
titleView = TitleView(frame: .zero)
super.init(frame: .zero)
setupChildViews(items: items)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Child View Initialization
private func setupChildViews(items: [ActionRow.Item]) {
titleView.siteIconView.tapped = { [weak self] in
QuickStartTourGuide.shared.visited(.siteIcon)
self?.titleView.siteIconView.spotlightIsShown = false
self?.delegate?.siteIconTapped()
}
titleView.siteIconView.dropped = { [weak self] images in
self?.delegate?.siteIconReceivedDroppedImage(images.first)
}
titleView.subtitleButton.addTarget(self, action: #selector(subtitleButtonTapped), for: .touchUpInside)
titleView.titleButton.addTarget(self, action: #selector(titleButtonTapped), for: .touchUpInside)
titleView.siteSwitcherButton.addTarget(self, action: #selector(siteSwitcherTapped), for: .touchUpInside)
titleView.translatesAutoresizingMaskIntoConstraints = false
addSubview(titleView)
let showsActionRow = items.count > 0
setupConstraintsForChildViews(showsActionRow)
}
// MARK: - Constraints
private var topActionRowConstraint: NSLayoutConstraint?
private func setupConstraintsForChildViews(_ showsActionRow: Bool) {
let constraints = constraintsForTitleView()
NSLayoutConstraint.activate(constraints)
}
private func constraintsForTitleView() -> [NSLayoutConstraint] {
return [
titleView.topAnchor.constraint(equalTo: topAnchor, constant: LayoutSpacing.top),
titleView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: LayoutSpacing.atSides),
titleView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -LayoutSpacing.atSides),
titleView.bottomAnchor.constraint(equalTo: bottomAnchor)
]
}
// MARK: - User Action Handlers
@objc
private func siteSwitcherTapped() {
delegate?.siteSwitcherTapped()
}
@objc
private func titleButtonTapped() {
QuickStartTourGuide.shared.visited(.siteTitle)
titleView.titleButton.shouldShowSpotlight = false
delegate?.siteTitleTapped()
}
@objc
private func subtitleButtonTapped() {
delegate?.visitSiteTapped()
}
// MARK: - Accessibility
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
refreshStackViewVisibility()
}
private func refreshStackViewVisibility() {
let showsActionRow = !traitCollection.preferredContentSizeCategory.isAccessibilityCategory
topActionRowConstraint?.constant = LayoutSpacing.betweenTitleViewAndActionRow(showsActionRow)
}
}
fileprivate extension BlogDetailHeaderView {
class TitleView: UIView {
private enum LabelMinimumScaleFactor {
static let regular: CGFloat = 0.75
static let accessibility: CGFloat = 0.5
}
private enum Dimensions {
static let siteIconHeight: CGFloat = 64
static let siteIconWidth: CGFloat = 64
static let siteSwitcherHeight: CGFloat = 36
static let siteSwitcherWidth: CGFloat = 36
}
private enum LayoutSpacing {
static let betweenTitleAndSubtitleButtons: CGFloat = 8
static let betweenSiteIconAndTitle: CGFloat = 16
static let betweenTitleAndSiteSwitcher: CGFloat = 16
static let betweenSiteSwitcherAndRightPadding: CGFloat = 4
static let subtitleButtonImageInsets = UIEdgeInsets(top: 1, left: 4, bottom: 0, right: 0)
static let rtlSubtitleButtonImageInsets = UIEdgeInsets(top: 1, left: -4, bottom: 0, right: 4)
}
// MARK: - Child Views
private lazy var mainStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [
siteIconView,
titleStackView,
siteSwitcherButton
])
stackView.alignment = .center
stackView.spacing = LayoutSpacing.betweenSiteIconAndTitle
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
let siteIconView: SiteIconView = {
let siteIconView = SiteIconView(frame: .zero)
siteIconView.translatesAutoresizingMaskIntoConstraints = false
return siteIconView
}()
let subtitleButton: SpotlightableButton = {
let button = SpotlightableButton(type: .custom)
button.titleLabel?.font = WPStyleGuide.fontForTextStyle(.footnote)
button.titleLabel?.adjustsFontForContentSizeCategory = true
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.minimumScaleFactor = LabelMinimumScaleFactor.regular
button.titleLabel?.lineBreakMode = .byTruncatingTail
button.setTitleColor(.primary, for: .normal)
button.accessibilityHint = NSLocalizedString("Tap to view your site", comment: "Accessibility hint for button used to view the user's site")
if let pointSize = button.titleLabel?.font.pointSize {
button.setImage(UIImage.gridicon(.external, size: CGSize(width: pointSize, height: pointSize)), for: .normal)
}
// Align the image to the right
if UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft {
button.semanticContentAttribute = .forceLeftToRight
button.imageEdgeInsets = LayoutSpacing.rtlSubtitleButtonImageInsets
} else {
button.semanticContentAttribute = .forceRightToLeft
button.imageEdgeInsets = LayoutSpacing.subtitleButtonImageInsets
}
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let titleButton: SpotlightableButton = {
let button = SpotlightableButton(type: .custom)
button.spotlightHorizontalPosition = .trailing
button.contentHorizontalAlignment = .leading
button.titleLabel?.font = AppStyleGuide.blogDetailHeaderTitleFont
button.titleLabel?.adjustsFontForContentSizeCategory = true
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.minimumScaleFactor = LabelMinimumScaleFactor.regular
button.titleLabel?.lineBreakMode = .byTruncatingTail
button.titleLabel?.numberOfLines = 1
button.accessibilityHint = NSLocalizedString("Tap to change the site's title", comment: "Accessibility hint for button used to change site title")
// I don't understand why this is needed, but without it the button has additional
// vertical padding, so it's more difficult to get the spacing we want.
button.setImage(UIImage(), for: .normal)
button.setTitleColor(.text, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let siteSwitcherButton: UIButton = {
let button = UIButton(frame: .zero)
let image = UIImage.gridicon(.chevronDown)
button.setImage(image, for: .normal)
button.contentMode = .center
button.translatesAutoresizingMaskIntoConstraints = false
button.tintColor = .gray
button.accessibilityLabel = NSLocalizedString("Switch Site", comment: "Button used to switch site")
button.accessibilityHint = NSLocalizedString("Tap to switch to another site, or add a new site", comment: "Accessibility hint for button used to switch site")
button.accessibilityIdentifier = "SwitchSiteButton"
return button
}()
private(set) lazy var titleStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [
titleButton,
subtitleButton
])
stackView.alignment = .leading
stackView.distribution = .equalSpacing
stackView.axis = .vertical
stackView.spacing = LayoutSpacing.betweenTitleAndSubtitleButtons
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupChildViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Configuration
func set(url: String) {
subtitleButton.setTitle(url, for: .normal)
}
// MARK: - Accessibility
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
refreshMainStackViewAxis()
}
// MARK: - Child View Setup
private func setupChildViews() {
refreshMainStackViewAxis()
addSubview(mainStackView)
pinSubviewToAllEdges(mainStackView)
setupConstraintsForSiteSwitcher()
}
private func refreshMainStackViewAxis() {
if traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
mainStackView.axis = .vertical
titleButton.titleLabel?.minimumScaleFactor = LabelMinimumScaleFactor.accessibility
subtitleButton.titleLabel?.minimumScaleFactor = LabelMinimumScaleFactor.accessibility
} else {
mainStackView.axis = .horizontal
titleButton.titleLabel?.minimumScaleFactor = LabelMinimumScaleFactor.regular
subtitleButton.titleLabel?.minimumScaleFactor = LabelMinimumScaleFactor.regular
}
}
// MARK: - Constraints
private func setupConstraintsForSiteSwitcher() {
NSLayoutConstraint.activate([
siteSwitcherButton.heightAnchor.constraint(equalToConstant: Dimensions.siteSwitcherHeight),
siteSwitcherButton.widthAnchor.constraint(equalToConstant: Dimensions.siteSwitcherWidth)
])
}
}
}